package cn.yitun.chat.yitunservermanagecore.general.manage.index.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserAccountDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.SecurityQuestionDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.userchat.SecurityQuestionInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.SecurityQuestionVo;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimbase.dao.system.UserDao;
import cn.yitun.chat.yitunserverimbase.dao.userchat.SecurityQuestionDao;
import cn.yitun.chat.yitunservermanagecore.general.manage.index.service.SecurityQuestionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SecurityQuestionServiceImpl
		extends CrudServiceImpl<SecurityQuestionDao, SecurityQuestionDto, SecurityQuestionInfo, SecurityQuestionVo>
		implements SecurityQuestionService {

	@Resource
	UserDao userDAO;

	/**
	 * 
	 * Date 2019-01-20 09:43:30<br>
	 * Description 保存密保问题
	 * 
	 * @author yitun
	 * @param list
	 * @param userId
	 * @since 1.0.0
	 */
	public void save(List<SecurityQuestionDto> list, Long userId) {
		if (null != list) {
			for (SecurityQuestionDto sq : list) {
				sq.setUserId(userId);
				dao.save(sq);
			}
		}
	}

	/**
	 * 
	 * Description <br>
	 * Date 2019-04-27 09:06:19<br>
	 * 
	 * @param account
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	public ResultMessage getSecurityQuestionDataListByAccount(String account) {
		ResultMessage message = new ResultMessage();
		UserVo userData = userDAO.getUserVoByAccount(account);
		if (null != userData) {
			List<SecurityQuestionVo> list = dao.listQuestionVO(
					R.param("userId", userData.getId()));
			if (list.isEmpty()) {
				message.addWarning(ExceptionEnum.SECURITY_QUESTION_NOT_EXISTS);
			}
			message.bodyPut("userId", userData.getId());
			message.bodyPut("list", list);
		} else {
			message.addWarning(ExceptionEnum.ACCOUNT_NOT_EXISTS);
		}
		return message;
	}

	/**
	 * 
	 * Date 2019-01-20 09:44:57<br>
	 * Description 根据保问题修改密码
	 * 
	 * @author yitun
	 * @param accountDto
	 * @return
	 * @since 1.0.0
	 */
	public Info updatePassword(UserAccountDto accountDto) {
		Long userId = accountDto.getUserId();
		String password = accountDto.getPassword();
		List<SecurityQuestionDto> answerList = accountDto.getList();
		Info info = new Info();
		boolean check = check(userId, answerList);
		if (check) {
			boolean mark = updatePassword(userId, password);
			if (!mark) {
				info.addWarning(ExceptionEnum.DO_FAIL);
			}
		} else {
			info.addWarning(ExceptionEnum.SECURITY_QUESTION_ANSWER_ERROR);
		}
		return info;
	}

	/**
	 * 更新密码
	 * @param userId
	 * @param password
	 * @return
	 */
	@Transactional
	public boolean updatePassword(Long userId, String password) {
		int mark = userDAO.updatePassword(userId, password);
		return mark > 0;
	}

	/**
	 * 校验
	 * @param userId
	 * @param answerList
	 * @return
	 */
	private boolean check(Long userId, List<SecurityQuestionDto> answerList) {
		boolean check = true;
		List<SecurityQuestionVo> questionList = dao.listVO(R.param("userId", userId));
		if (null != questionList && !questionList.isEmpty() && null != answerList && !answerList.isEmpty()) {
			int answerSize = answerList.size();
			int questionSize = questionList.size();
			// 如果回答数量和问题数量不一致，则回答错误
			if (answerSize == questionSize) {
				Map<Long, String> map = new HashMap<>();
				for (SecurityQuestionDto sq : answerList) {
					map.put(sq.getId(), sq.getAnswer());
				}
				for (SecurityQuestionVo sq : questionList) {
					Long id = sq.getId();
					String answer = sq.getAnswer();
					//如果其中一个回答错误，则回答错误
					if (null == answer || !answer.equals(map.get(id))) {
						check = false;
						break;
					}
				}
			} else {
				check = false;
			}

		} else {
			check = false;
		}
		return check;
	}
}
